/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_Counter.
	/// </summary>
	public class Block_Counter : Block
	{

		private string _counterVar;
		private string _startValue;
		private string _endValue;
		private string _stepValue;
		private int _initializer;

		public override TYPE GetBlockType()
		{
			return TYPE.Counter;
		}

		/// <summary>
		/// This block is bigger than normal, return bigger size.
		/// </summary>
		/// <returns></returns>
		public override int GetWidth()
		{
			return 128;
		}


		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_Counter::GetXML Started.");	
			// <Counter name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<Counter name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			// ...counter="COUNTER"...
			xml += " counter=\"" + Utils.XmlString(_counterVar) + "\"";
			// ...lower="START_VALUE"...
			xml += " lower=\"" + Utils.XmlString(_startValue) + "\"";
			// ...upper="END_VALUE"...
			xml += " upper=\"" + Utils.XmlString(_endValue) + "\"";
			// ...step="STEP_SIZE" >
			xml += " step=\"" + Utils.XmlString(_stepValue) + "\">\r\n";

			// This block is made up of more than one block in the .seq file, so we
			// check if we are an initializer or a stepper block. The other input
			// which is a dangler can be set to 0.
			// <Inputs>
			xml += "\t\t\t\t<Inputs>\r\n";
			// <Input _id="BLOCK_ID" name="">
			if (_initializer == 1)
			{
				xml += "\t\t\t\t\t<Input _id=\"" + _blockId + "\" name=\"Start\"/>\r\n";
				xml += "\t\t\t\t\t<Input _id=\"" + 0 + "\" name=\"Step\"/>\r\n";
			}
			else
			{
				xml += "\t\t\t\t\t<Input _id=\"" + 0 + "\" name=\"Start\"/>\r\n";
				xml += "\t\t\t\t\t<Input _id=\"" + _blockId + "\" name=\"Step\"/>\r\n";
			}
			// </Inputs>
			xml += "\t\t\t\t</Inputs>\r\n";
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// </Counter>
			xml +="\t\t\t</Counter>\r\n";
			return xml;
		}

		public Block_Counter(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
			Trace.Debug("Block_Counter created.");

			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
			outputErrorIds.Add(new BlockOutput(OUTPUT.Overflow, 123));

			_initializer = rawData[_currentPos + 16];

			_currentPos += 20;
	
			// Counter variable
			_counterVar = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// Start value
			_startValue = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// end value
			_endValue = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// step value
			_stepValue = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			Trace.Debug("Block_Counter: counter: " + _counterVar +
				", startVal: " + _startValue +
				", endVal: " + _endValue +
				", stepVal: " + _stepValue +
				", init: " + _initializer);
		}

	}

}